package com.example.deploysinggle.test1.common.util;

import lombok.extern.slf4j.Slf4j;

import java.security.MessageDigest;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2024/12/25 16:34
 */
@Slf4j
public class MD5Util {

    // 获取字符串的 MD5 值
    public static String calculateMD5(String input) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            byte[] messageDigestBytes = messageDigest.digest(input.getBytes());
        StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigestBytes) {
                 String hex = Integer.toHexString(0xff & b);
                 if(hex.length() ==1){
                     hexString.append('0');
                 }
                 hexString.append(hex);

            }
            return hexString.toString();
        } catch (Exception e) {
           log.error("md5 failed : input:{}, exception:{}",input,e);
            return null;
        }
    }

}
